package com.hdoit.kylin.logical.document.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.LoginModel;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.tennetcn.component.web.SessionWebFactory;
import com.hdoit.kylin.logical.document.dao.IReportCataLogDao;
import com.hdoit.kylin.logical.document.model.ReportCataLog;
import com.hdoit.kylin.logical.document.model.ReportCataLogEmployee;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.document.ReportCataLogSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;

/**
 * @author guoshuren
 * @email
 * @createtime 2018年02月01日 20:09:47
 * @comment
 */
@Component
public class ReportCataLogDaoImpl extends SuperDao<ReportCataLog> implements
		IReportCataLogDao {

	@Override
	public List<ReportCataLog> queryList(ReportCataLogSearch search,
			PagerModel pager) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		finderExpression.select(" report.*")
				.from(ReportCataLog.class, "report")
				.addOrder("report.createDate", OrderEnum.desc)
				.addOrder("report.createUserName", OrderEnum.desc);

		appendFinder(finderExpression, search);

		if (pager == null) {
			return queryList(finderExpression, search);
		}
		return queryList(finderExpression, search, pager);
	}

	private void appendFinder(IFinderExpression finderExpression,
			ReportCataLogSearch search) {

		if (search.getReportType() != null
				&& !search.getReportType().equals("all")) {
			finderExpression.andWhere("reportType = #{reportType}").setParam(
					"reportType", search.getReportType());
		}
		if (search.getId() != null) {
			finderExpression.andWhere("id=#{id}")
					.setParam("id", search.getId());
		}
		if (search.getEmployeeIds() != null
				&& search.getEmployeeIds().size() > 0) {
			finderExpression.andWhereInString("report.createUserId",
					search.getEmployeeIds());
		}
		finderExpression.andWhere("deleteMark = #{deleteMark}").setParam(
				"deleteMark", YesOrNoInteger.no);

	}

	@Override
	public List<ReportCataLog> queryListByTeacherId(String teacherId) {
		ITeacherStudentService teacherStudentService = CommonApplicationContextUtil
				.getCurrentContext().getBean(ITeacherStudentService.class);

		ReportCataLogSearch search = new ReportCataLogSearch();
		List<String> employeeIds = new ArrayList<String>();
		employeeIds.add(teacherId);

		TeacherStudentSearch teacherStudentSearch = new TeacherStudentSearch();
		teacherStudentSearch.setTeacherId(teacherId);
		List<TeacherStudent> students = teacherStudentService.queryList(
				teacherStudentSearch, null);
		for (TeacherStudent student : students) {
			employeeIds.add(student.getStudentId());
		}

		search.setEmployeeIds(employeeIds);
		return queryList(search, null);

	}

	@Override
	public List<ReportCataLog> queryListByReportCataLogIds(
			List<String> reportIds) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		if (reportIds != null && reportIds.size() > 0) {
			finderExpression.select("report.*")
					.from(ReportCataLog.class, "report")
					.addOrder("createDate", OrderEnum.desc)
					.andWhereInString("id", reportIds)
					.andWhere("deleteMark = #{deleteMark}")
					.setParam("deleteMark", YesOrNoInteger.no);

			return queryList(finderExpression);
		}
		return new ArrayList<ReportCataLog>();
	}
}
